package com.unknowpupil.alibaba.cloud.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 *  结果返回集。
 * @Classname: ResponseResult
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/1/16 13:23
 * @Description:
 * @Version: 1.0
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> implements Serializable {
    private volatile static ResponseResult result;
    private static final long serialVersionUID = 6304138315884288364L;

    @ApiModelProperty(value = "状态")
    private Boolean success;

    @ApiModelProperty(value = "返回状态码")
    private Integer code;

    @ApiModelProperty(value = "返回对象")
    private T data;

    @ApiModelProperty(value = "返回正确信息")
    private String message;

    @ApiModelProperty(value = "返回错误信息")
    private String msg;
    /**
     * 功能描述: <br>
     * 〈 默认成功 〉
     *
     * @param <T> {@link T}  泛型
     * @auther: w10g
     * @Date: 2020/1/16 13:59
     * @since: 1.0.0
     * @return: {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> buildSuccess() {
        build();
        result.setSuccess(true);
        result.setCode(ErrorState.SUCCESS.getCode());
        result.setData(null);
        result.setMessage(ErrorState.SUCCESS.getMessage());
        result.setMsg(null);
        return result;
    }

    /**
     * 功能描述: <br>
     * 〈 返回成功参数 〉
     *
     * @auther: w10g
     * @Date: 2020/1/16 14:01
     * @since: 1.0.0
     * @Param: data {@link T}  成功数据
     * @return: {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> buildSuccess(T data) {
        build();
        result.setSuccess(true);
        result.setCode(ErrorState.SUCCESS.getCode());
        result.setMessage(ErrorState.SUCCESS.getMessage());
        result.setData(data);
        result.setMsg(null);
        return result;
    }

    /**
     * 功能描述: <br>
     * 〈  自定义状态信息 〉
     *
     * @auther: w10g
     * @Date: 2020/1/16 14:01
     * @since: 1.0.0
     * @Param: enums {@link ErrorState}  自定义成功状态信息
     * @return: {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> buildSuccess(ErrorState enums) {
        build();
        result.setSuccess(true);
        result.setCode(enums.getCode());
        result.setMessage(enums.getMessage());
        result.setData(null);
        result.setMsg(null);
        return result;
    }


    /**
     * 功能描述: <br>
     * 〈 返回成功参数 && 自定义状态信息 〉
     *
     * @auther: w10g
     * @Date: 2020/1/16 14:01
     * @since: 1.0.0
     * @Param: enums {@link ErrorState}  自定义成功状态信息
     * @Param: data {@link T}  成功数据
     * @return: {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> buildSuccess(ErrorState enums, T data) {
        build();
        result.setSuccess(true);
        result.setCode(enums.getCode());
        result.setMessage(enums.getMessage());
        result.setData(data);
        result.setMsg(null);
        return result;
    }


    /**
     * 功能描述: <br>
     * 〈 返回默认错误信息 〉
     *
     * @param <T> {@link <T>}  泛型
     * @auther: w10g
     * @Date: 2020/1/16 14:09
     * @since: 1.0.0
     * @return: {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> buildFailure() {
        build();
        result.setSuccess(false);
        result.setCode(ErrorState.COMMON_FAIL.getCode());
        result.setMsg(ErrorState.COMMON_FAIL.getMessage());
        result.setData(null);
        result.setMessage(null);
        return result;
    }

    /**
     * 功能描述: <br>
     * 〈 〉
     *
     * @param enums {@link ErrorState} 自定义错误状态
     * @param <T>   {@link T} 泛型
     * @auther: w10g
     * @Date: 2020/1/16 14:11
     * @since: 1.0.0
     * @return: {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> buildFailure(ErrorState enums) {
        build();
        result.setSuccess(false);
        result.setCode(enums.getCode());
        result.setMsg(enums.getMessage());
        result.setData(null);
        result.setMessage(null);
        return result;
    }
    /**
     * 功能描述: <br>
     * 〈 系统异常信息 〉
     *
     * @param code {@code Integer} 系统错误码
     * @param message {@code String} 系统错误信息
     * @param <T>   {@link T} 泛型
     * @auther: w10g
     * @Date: 2020/1/16 14:11
     * @since: 1.0.0
     * @return: {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> buildFailure(Integer code ,String message) {
        build();
        result.setSuccess(false);
        result.setCode(code);
        result.setMsg(message);
        result.setData(null);
        result.setMessage(null);
        return result;
    }
    public static <T> ResponseResult<T> buildFailure(String message) {
        build();
        result.setSuccess(false);
        result.setCode(201);
        result.setMsg(message);
        result.setData(null);
        result.setMessage(null);
        return result;
    }


    /**
     * 功能描述: <br>
     * 〈 系统异常信息 && 错误数据  〉
     *
     * @param enums {@lind ResultCode} 系统状态
     * @param <T>   {@link T} 泛型
     * @param data  {@link T} 错误数据
     * @auther: w10g
     * @Date: 2020/1/16 14:11
     * @since: 1.0.0
     * @return: {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> buildFailure(ErrorState enums, T data) {
        build();
        result.setSuccess(false);
        result.setCode(enums.getCode());
        result.setMsg(enums.getMessage());
        result.setMessage(null);
        result.setData(data);
        return result;
    }

    /**
     * 登录授权的异常
     * @param msg
     * @return
     */
    public static <T> ResponseResult<T> authorizationFailed(String msg){
        build();
        result.setSuccess(false);
        result.setCode(ErrorState.NO_AOUTH_ACCESS.getCode());
        result.setMsg(msg);
        result.setMessage(null);
        result.setData(null);
        return result;
    }

   /**
    * 功能描述: <br>
    * 〈 私有化构造方法 〉
    *
    * @auther: w10g
    * @Date: 2020/1/16 16:03
    * @since: 1.0.0
    * @return:  {@link ResponseResult }
    */
    private ResponseResult() {}
    /**
     * 功能描述: <br>
     * 〈 实例化实体 〉
     *
     * @auther: w10g
     * @Date: 2020/1/16 16:03
     * @since: 1.0.0
     * @Param:  <T> {@link T} 泛型
     * @return:
     */
    private static <T> ResponseResult<T> build() {
        if (result == null) {
            synchronized (ResponseResult.class) {
                if (result == null) {
                    result = new ResponseResult();
                }
            }
        }
        return result;
    }


}
